本文以C Link SDK中的Demo文件./demos/tunnel_switch_demo.c
为例,介绍业务进程与远程登录进程隔离中业务进程的处理过程以及相关日志。
业务进程处理流程图
操作步骤
初始化远程登录隧道开关模块。
/* 创建1个RA实例并内部初始化默认参数 */ void *tunnel_switch_handle = aiot_tunnel_switch_init(); if (tunnel_switch_handle == NULL) { printf( "aiot_tunnel_switch_init failed\n"); return -1; }
配置功能。
关联MQTT模块。MQTT模块需要处于连接状态,具体操作,请参见MQTT接入。
配置消息回调函数。
回调函数功能:解析隧道相关消息,并将消息同步给运维进程。
/* 配置MQTT句柄,ra内部会订阅MQTT的消息 */ aiot_tunnel_switch_setopt(tunnel_switch_handle, AIOT_TSOPT_MQTT_HANDLE, mqtt_handle);
/* 配置消息回调函数 */ aiot_tunnel_switch_setopt(tunnel_switch_handle, AIOT_TSOPT_RECV_HANDLER, demo_tunnel_switch_recv_handle);
请求隧道建连信息。
说明请求的隧道信息,会在七天后过期。详情请参见:物联网平台远程登录。
/* 设备主动请求隧道建连,该隧道只用于远程登录 */ aiot_tunnel_switch_request(tunnel_switch_handle);
进程间通信传递给运维进程。
以下示例进程间通信使用的是Linux的消息队列,您也可替换为其它进程间通信方式:
static void demo_tunnel_switch_recv_handle(void *handle, const aiot_tunnel_switch_recv_data_t *recv, void *userdata) { demo_tunnel_info_t info; memset(&info, 0, sizeof(info)); if(recv->operation != AIOT_TUNNEL_OPERATOPN_CLOSE) { /* 输出隧道认证过期的时间,隧道邻近过期的话,需要提前请求 */ printf("tunnel %s, create time %"PRIu64" , will expired after %"PRIu64" seconds \r\n", recv->tunnel_id, recv->created_time, recv->expired_time); } else { printf("tunnel %s close , close reason %s \r\n", recv->tunnel_id, recv->close_reason); } info.operation = recv->operation; if(recv->tunnel_id != NULL) { memcpy(info.tunnel_id, recv->tunnel_id, strlen(recv->tunnel_id)); } if(recv->host != NULL) { memcpy(info.host, recv->host, strlen(recv->host)); } if(recv->port != NULL) { memcpy(info.port, recv->port, strlen(recv->port)); } if(recv->path != NULL) { memcpy(info.path, recv->path, strlen(recv->path)); } if(recv->token != NULL) { memcpy(info.token, recv->token, strlen(recv->token)); } /* 将隧道操作及建连信息发送给隧道业务进程,也可固化在文件中实现进程间通信 */ demo_posix_sysmsg_send((char *)&info, sizeof(info)); }
运行日志
建立MQTT连接。
[1644818817.044][LK-0313] MQTT user calls aiot_mqtt_connect api, connect [1644818817.044][LK-032A] mqtt host: iot-test-*************.unify.aliyuncs.com [1644818817.044][LK-0317] user name: tunnel_test_00**********LXZe [1644818817.044][LK-0318] password: 89EF89F414F1369*******************B18B39A6B63A3EFB319A0C408 success to establish tcp, fd=3 local port: 35024 [1644818817.133][LK-1000] establish mbedtls connection with server(host='iot-test-************.unify.aliyuncs.com', port=[443]) [1644818817.144][LK-1000] success to establish mbedtls connection, (cost 45247 bytes in total, max used 47983 bytes) [1644818817.611][LK-0313] MQTT connect success in 567 ms AIOT_MQTTEVT_CONNECT
隧道开关模块初始化完成。
[1644818817.611][LK-0309] sub: /sys/a1******XZe/tunnel_test_001/secure_tunnel/notify [1644818817.611][LK-0309] sub: /sys/a1******XZe/tunnel_test_001/secure_tunnel/proxy/request_reply heartbeat response suback, res: -0x0000, packet id: 2, max qos: 1 suback, res: -0x0000, packet id: 1, max qos: 1
隧道建连信息请求。
[1644818817.611][LK-0309] pub: /sys/a1W*******XZe/tunnel_test_001/secure_tunnel/proxy/request [LK-030A] > 7B 22 69 64 22 3A 31 30 30 30 2C 22 70 61 72 61 | {"id":1000,"para [LK-030A] > 6D 73 22 3A 7B 7D 7D | ms":{}}
隧道建连信息回复。
[1644818818.122][LK-0309] pub: /sys/a1******XZe/tunnel_test_001/secure_tunnel/proxy/request_reply [LK-030A] < 7B 22 63 6F 64 65 22 3A 32 30 30 2C 22 64 61 74 | {"code":200,"dat [LK-030A] < 61 22 3A 7B 22 73 63 68 65 6D 61 22 3A 22 77 73 | a":{"schema":"ws [LK-030A] < 73 22 2C 22 70 61 74 68 22 3A 22 2F 74 75 6E 6E | s","path":"/tunn [LK-030A] < 65 6C 2F 34 61 32 66 63 39 39 62 2D 38 32 66 39 | el/4a2f********9 [LK-030A] < 2D 34 36 65 65 2D 62 34 31 63 2D 34 63 36 30 31 | -46ee-b41c-4c601 [LK-030A] < 36 33 30 65 35 31 61 2F 64 65 73 74 22 2C 22 74 | 630e51a/dest","t [LK-030A] < 6F 6B 65 6E 5F 65 78 70 69 72 65 22 3A 36 30 34 | oken_expire":604 [LK-030A] < 37 39 39 2C 22 74 75 6E 6E 65 6C 5F 69 64 22 3A | 799,"tunnel_id": [LK-030A] < 22 34 61 32 66 63 39 39 62 2D 38 32 66 39 2D 34 | "4a2fc********** [LK-030A] < 36 65 65 2D 62 34 31 63 2D 34 63 36 30 31 36 33 | 6ee-b41c-4c60163 [LK-030A] < 30 65 35 31 61 22 2C 22 70 61 79 6C 6F 61 64 5F | 0e51a","payload_ [LK-030A] < 6D 6F 64 65 22 3A 22 61 70 70 2D 70 72 6F 78 79 | mode":"app-proxy [LK-030A] < 22 2C 22 70 6F 72 74 22 3A 34 34 33 2C 22 68 6F | ","port":443,"ho [LK-030A] < 73 74 22 3A 22 69 6F 74 2D 73 65 63 75 72 65 2D | st":"iot-secure- [LK-030A] < 74 75 6E 6E 65 6C 2D 68 7A 2D 70 72 65 2E 61 6C | tunnel-hz-pre.al [LK-030A] < 69 79 75 6E 63 73 2E 63 6F 6D 22 2C 22 75 72 69 | iyuncs.com","uri [LK-030A] < 22 3A 22 77 73 73 3A 2F 2F 69 6F 74 2D 73 65 63 | ":"wss://iot-sec [LK-030A] < 75 72 65 2D 74 75 6E 6E 65 6C 2D 68 7A 2D 70 72 | ure-tunnel-hz-pr [LK-030A] < 65 2E 61 6C 69 79 75 6E 63 73 2E 63 6F 6D 2F 74 | e.aliyuncs.com/t [LK-030A] < 75 6E 6E 65 6C 2F 34 61 32 66 63 39 39 62 2D 38 | unnel/4a2******* [LK-030A] < 32 66 39 2D 34 36 65 65 2D 62 34 31 63 2D 34 63 | 2f9-46ee-b41c-4c [LK-030A] < 36 30 31 36 33 30 65 35 31 61 2F 64 65 73 74 22 | 601630e51a/dest" [LK-030A] < 2C 22 74 6F 6B 65 6E 22 3A 22 32 30 39 34 65 61 | ,"token":"2094ea [LK-030A] < 37 37 2D 34 32 61 38 2D 34 32 37 32 2D 38 63 38 | 77-42a8-4272-8c8 [LK-030A] < 65 2D 62 37 61 65 36 64 37 39 65 62 30 36 22 7D | e-b7**********6"} [LK-030A] < 2C 22 69 64 22 3A 22 31 30 30 30 22 2C 22 6D 65 | ,"id":"1000","me [LK-030A] < 73 73 61 67 65 22 3A 22 73 75 63 63 65 73 73 22 | ssage":"success" [LK-030A] < 7D | } [1644818818.122][LK-0000] tunnel_switch_topic_handler payload:{"code":200,"data":{"schema":"wss","path":"/tunnel/4a2f************ee-b41c-4c601630e51a/dest","tok tunnel 4a2f***********46ee-b41c-4c601630e51a, create time 1644818818127 , will expired after 604799 seconds
进程间通信发送指令。
send msgsnd 644
文档内容是否对您有帮助?